package modules.majiang.view
{
	/**
	 *
	 * @author warden_feng
	 * 2013-5-7
	 */
	import com.feng.components.FButton;
	import com.netease.protobuf.UInt64;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import modules.GameDispatcher;
	import modules.GameEvent;
	import modules.load.Load;
	import modules.lobby.LobbyEvent;
	import modules.lobby.data.LobbyDataModel;
	import modules.lobby.data.NormalTableModel;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;

	public class GameTopComponet extends Sprite
	{
		private var gameTopUI:MovieClip;

		public function GameTopComponet()
		{
			super();
			
			gameTopUI = Load.getInstance("fla.view.majiang.GameTopUI");
			
			addChild(gameTopUI);
		}

		private var selectedbuttonObject:Object;

		private var buttonObjectList:Array = [];

		/**
		 * 是否是多窗口模式中
		 */
		[Bindable]
		public static var isMoreViewModle:Boolean = false;

		[Bindable]
		private var tableId:UInt64;

		[Bindable]
		private var tableDataM:TableDataModel;

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		public function onShowTabTable(event:GameEvent):void
		{
			var tableIdStr:String = event.data.tableIdStr;
			for each (var buttonObject:Object in buttonObjectList)
			{
				if (buttonObject.tableIdStr == tableIdStr)
				{
					var button:FButton = buttonObject.button;
					button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}
			}
		}

		public function onShowTabLobby(event:GameEvent):void
		{
			for each (var buttonObject:Object in buttonObjectList)
			{
				if (buttonObject.isLobby)
				{
					var button:FButton = buttonObject.button;
					button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}
			}
		}

		/**
		 * 添加大厅按钮
		 */
		public function addLobbyButton():void
		{
			var name:String = "Lobby";
			if (!hasButton(name))
			{
				var buttonObject:Object = {name: name, isLobby: true};
				createButton(buttonObject);
			}
		}

		/**
		 * 添加多视图按钮
		 */
		public function addMoreViewButton():void
		{
			var name:String = "more";
			if (!hasButton(name))
			{
				var buttonObject:Object = {name: name, isMoreView: true};
				createButton(buttonObject);
			}
		}

		public function addTableButton(tableIdStr:String):void
		{
			var name:String = "table" + tableIdStr;
			var normalTableModel:NormalTableModel = LobbyDataModel.cashTableDic[tableIdStr];
			if (normalTableModel)
			{
				name = normalTableModel.tableName;
			}
			if (!hasButton(name))
			{
				var buttonObject:Object = {name: name, isTable: true, tableIdStr: tableIdStr};
				createButton(buttonObject);
			}

			//重新添加一次, 使得再最上层
			if (this.parent)
			{
				this.parent.addChild(this);
			}
		}

		public function onLeaveTable(tableIdStr:String):void
		{
			removeTableButton(tableIdStr);

			var isMoreView:Boolean = false;

			for (var i:int = 0; i < buttonObjectList.length; i++)
			{
				var buttonObject:Object = buttonObjectList[i];
				if (buttonObject.button.selected == true)
				{
					if (buttonObject.name == "more")
					{
						isMoreView = true;
						break;
					}
				}
			}

			var tableId:UInt64 = TablesDataManager.tableDataModeDic[tableIdStr].tableDataFromLobby.tableID;
			UIAllRefer.instance.getGameUIbyTableId(tableId).close();

			if (isMoreView && UIAllRefer.instance.getCurrentShowGameUi() != null)
			{
				UIAllRefer.instance.updateMoreGameUI();
			}
			else
			{
				dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_SHOW));
				GameTopComponet.isMoreViewModle = false;
			}

		}

		public function onAddReplayButton(event:GameEvent):void
		{
			var replayId:String = event.data.replayId;
			var name:String = "replay" + replayId;
			if (!hasButton(name))
			{
				var buttonObject:Object = {name: name, isReplay: true, replayId: replayId};
				createButton(buttonObject, false);
			}
		}

		private function hasButton(name:String):Boolean
		{
			var has:Boolean = buttonObjectList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.name == name;
			}).length > 0;
			return has;
		}

		private function createButton(buttonObject:Object, select:Boolean = true):void
		{
			var button:FButton;
			if (buttonObject.name == "Lobby")
			{
				button = FButton.getInstance(gameTopUI.lobbyBtn);
			}
			else
			{
				button = getRoomBtn(buttonObject.name);

				var buttonX:Number = 0;
				if (gameTopUI.roomIconBox.numChildren > 1)
				{
					var lastButton:DisplayObject = gameTopUI.roomIconBox.getChildAt(gameTopUI.roomIconBox.numChildren - 1);
					buttonX = lastButton.x + lastButton.width;
				}
				button.x = buttonX;
				gameTopUI.roomIconBox.addChild(button.skin);
			}

			button.addEventListener(MouseEvent.CLICK, onClick);

			buttonObject.button = button;
			buttonObjectList.push(buttonObject);

			select && selectButtonObject(buttonObject);
		}

		/**
		 * 移除桌子按钮
		 * @param tableId 桌子编号
		 */
		private function removeTableButton(tableIdStr:String):void
		{
			var name:String = "table" + tableIdStr;
			var normalTableModel:NormalTableModel = LobbyDataModel.cashTableDic[tableIdStr];
			if (normalTableModel)
			{
				name = normalTableModel.tableName;
			}
			for (var i:int = 0; i < buttonObjectList.length; i++)
			{
				var buttonObject:Object = buttonObjectList[i];
				if (buttonObject.name == name)
				{
					var button:FButton = buttonObject.button;
					gameTopUI.roomIconBox.removeChild(button.skin);
					buttonObjectList.splice(i, 1);
					button.removeEventListener(MouseEvent.CLICK, onClick);
					break;
				}
			}
		}

		/**
		 * 点击按钮
		 */
		private function onClick(event:MouseEvent):void
		{
			for each (var buttonObject:Object in buttonObjectList)
			{
				if (buttonObject.button == event.target)
				{
					if (buttonObject.isLobby)
					{
						isMoreViewModle = false;

						logger("显示大厅");

						selectButtonObject(buttonObject)

					}
					else if (buttonObject.isTable)
					{
						isMoreViewModle = false;

						var tableIdStr:String = buttonObject.tableIdStr;
						logger("显示桌子：" + tableIdStr);

						var tableId:UInt64 = TablesDataManager.tableDataModeDic[tableIdStr].tableDataFromLobby.tableID;
						if (UIAllRefer.instance.getGameUIbyTableId(tableId).visible == false)
						{
							var start:uint = getTimer();
							dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, {tableId: tableId}));
							logger("切换桌子花费时间：" + (getTimer() - start));
						}
						selectButtonObject(buttonObject);
					}
					else if (buttonObject.isReplay)
					{
						var replayId:String = buttonObject.replayId;
						logger("播放录像：" + replayId);

						dispatcher.dispatchEvent(new GameEvent(GameEvent.DO_REPLAY, {replayId: replayId}));
						selectButtonObject(buttonObject);
					}
					else if (buttonObject.isMoreView)
					{
						isMoreViewModle = true;

						logger("更多视图");
						selectButtonObject(buttonObject);
					}
					break;
				}
			}
		}

		private function selectButtonObject(buttonObject:Object):void
		{
			var selectedButton:FButton;
			if (selectedbuttonObject != null)
			{
				selectedButton = selectedbuttonObject.button;
				selectedButton.selected = false;
				selectedButton.enabled = true;
			}
			selectedbuttonObject = buttonObject;
			if (selectedbuttonObject != null)
			{
				selectedButton = selectedbuttonObject.button;
				selectedButton.selected = true;
				selectedButton.enabled = false;

				if (selectedbuttonObject.isLobby)
				{
					dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_SHOW));
					this.visible = false;
					UIAllRefer.instance.resetGameUi();
				}
				else if (selectedbuttonObject.isTable)
				{
					this.visible = true;
					dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_CLOSE));
					UIAllRefer.instance.resetGameUi();
					var normalTableModel:NormalTableModel = TablesDataManager.tableDataModeDic[selectedbuttonObject.tableIdStr].tableDataFromLobby;
					tableId = normalTableModel.tableID;
					UIAllRefer.instance.getGameUIbyTableId(tableId).show();
					tableDataM = TablesDataManager.tableDataModeDic[selectedbuttonObject.tableIdStr];
				}
				else if (selectedbuttonObject.isMoreView)
				{
					this.visible = true;
					dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_CLOSE));
					UIAllRefer.instance.showMoreGameUI();
				}
			}
		}

		//创建并返回一个RoomBtn
		public function getRoomBtn(btnName:String):FButton
		{
			var btn:FButton = FButton.getInstance(new RoomBtn());
			btn.label = btnName == "more" ? btnName : btnName.substring(btnName.length - 1);
			return btn;
		}
		
		private function get RoomBtn():Class
		{
			return Load.getInstance("fla.view.majiang.RoomBtn");
		}
	}
}
